package album

import (
	"coarse_score_cal_flow/model"
	"math/big"
	"net/http"

	"github.com/gin-gonic/gin"
)

var albums = []model.Album{
	{ID: "1", Title: "Blue Train", Artist: "Alpha", Price: 56.99},
	{ID: "2", Title: "Jeru", Artist: "Beta", Price: 17.99},
	{ID: "3", Title: "Sarah Vaughan and Clifford Brown", Artist: "Charlie", Price: 39.99},
	{ID: "4", Title: "Blue", Artist: "Alpha", Price: 56.99},
}

func GetAlbums(c *gin.Context) {
	c.IndentedJSON(http.StatusOK, albums)
}

func GetAlbumsByNameUrl(c *gin.Context) {
	name := c.Param("name")
	for _, album := range albums {
		if album.Artist == name {
			c.IndentedJSON(http.StatusOK, album)
		}
	}
}

func PrintAllAction(c *gin.Context) {
	name := c.Param("name")
	action := c.Param("action")

	c.IndentedJSON(http.StatusOK, map[string]interface{}{
		"name":   name,
		"action": action,
	})
}

func GetQuery(c *gin.Context) {
	// with default
	name := c.DefaultQuery("name", "Alpha")

	// no default
	price := new(big.Float)
	price.SetString(c.Query("price"))
	priceFromBig, _ := price.Float64()

	// 不return匹配多个条件都会输出
	for _, album := range albums {
		if album.Artist == name && album.Price == priceFromBig {
			c.JSON(http.StatusOK, gin.H{
				"name":  name,
				"price": price,
			})
			return
		}
	}
}

func GetQueryError(c *gin.Context) {
	// with default
	name := c.DefaultQuery("name", "Alpha")

	// no default
	price := new(big.Float)
	price.SetString(c.Query("price"))
	priceFromBig, _ := price.Float64()

	// 不return匹配多个条件都会输出
	for _, album := range albums {
		if album.Artist == name && album.Price == priceFromBig {
			c.IndentedJSON(http.StatusOK, album)
			return
		}
	}
}
